package com.engine.modules.ytht.controller;

import com.alibaba.fastjson.JSONObject;
import com.engine.common.baseEntity.ResponseEntity.ResponseMsg;
import com.engine.common.util.ListMapAndJson;
import com.engine.modules.speapi.mapper.SpecialGisMapper;
import com.engine.modules.ytht.entity.LocationEntity;
import com.engine.modules.ytht.entity.LocationResultEntity;
import com.engine.modules.ytht.mapper.LocationMapper;
import com.engine.modules.ytht.service.LocationService;
import com.engine.modules.ytht.service.LocationServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiOperationSupport;
import io.swagger.annotations.ApiSort;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.CacheManager;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

@RestController
@Api(tags = "4、行政区划相关接口")
@ApiSort(value = 4)
@CrossOrigin
@RequestMapping("/Location")
@Slf4j
public class LocationController {

    @Autowired
    LocationMapper locationMapper;

    @Autowired
    LocationServiceImpl locationService;

    @Autowired
    CacheManager cacheManager;

    @ApiOperation(value = "通过地名查询点位置", notes = "通过地名查询点位置（LIKE查询）")
    @GetMapping(value = "/SearchLocationFastByName")
    @ApiOperationSupport(order = 1)
    public ResponseMsg SearchFastByName(@RequestParam(value = "name") String name,
                                        HttpServletResponse response) {
        try {
            List<LocationEntity> mapList = locationMapper.SearchLocationFast(name);
            return ResponseMsg.success("查询成功，共获取" + mapList.size() + "条数据").addData(mapList);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return ResponseMsg.error("系统错误，e：" + e.getMessage());
        }
    }

    @ApiOperation(value = "通过地名查询点位置,包含上级信息", notes = "通过地名查询点位置（LIKE查询），包含上级信息")
    @GetMapping(value = "/SearchLocationFullByName")
    @ApiOperationSupport(order = 2)
    public ResponseMsg SearchLocationFullByName(@RequestParam(value = "name") String name,
                                           @RequestParam(value = "pageSize",defaultValue = "5",required = false) Integer pageSize,
                                           @RequestParam(value = "pageNum",defaultValue = "1",required = false) Integer pageNum,
                                           HttpServletResponse response) {
        try {
            LocationResultEntity locationResultEntity = locationMapper.SearchLocationFull(name,pageSize,pageNum);
            return ResponseMsg.success("查询成功，共获取" + locationResultEntity.getLocationEntityList().size() + "条数据").addData(locationResultEntity);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return ResponseMsg.error("系统错误，e：" + e.getMessage());
        }
    }

    @ApiOperation(value = "根据PAC查询行政区划geojson", notes = "输入pac获取对应的行政区划geojson")
    @GetMapping(value = "/getLocationByPac/{pac}")
    public ResponseMsg getLocationByPac(@PathVariable("pac") String pac, HttpServletResponse response) {
        try {
            if (pac.length() < 12) {
                int x = 12 - pac.length();
                for (int i = 0; i < x; i++)
                    pac += "0";
            }
            List<Map<String, Object>> locationPolygon2 = locationService.getLocationPolygon2(pac);
            JSONObject jsonObject = ListMapAndJson.ListMap2GeojsonObject(locationPolygon2);
            return ResponseMsg.success("查询成功").addData(jsonObject);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return ResponseMsg.error("系统错误，e：" + e.getMessage());
        }
    }

    @ApiOperation(value = "根据烟草PAC查询行政区划geojson", notes = "输入烟草pac获取对应的行政区划geojson")
    @GetMapping(value = "/getLocationByYCPac/{ycpac}")
    public ResponseMsg getLocationByYCPac(@PathVariable("ycpac") String ycpac, HttpServletResponse response) {
        try {
//            if (ycpac.length() < 12) {
//                int x = 12 - ycpac.length();
//                for (int i = 0; i < x; i++)
//                    ycpac += "0";
//            }
            List<Map<String, Object>> locationPolygon2 = locationService.getLocationPolygonByYCPac(ycpac);
            JSONObject jsonObject = ListMapAndJson.ListMap2GeojsonObject(locationPolygon2);
            return ResponseMsg.success("查询成功").addData(jsonObject);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return ResponseMsg.error("系统错误，e：" + e.getMessage());
        }
    }

    @ApiOperation(value = "根据经纬度查询行政区划", notes = "输入lng(x经度),lat(y纬度)获取对应的行政区划geojson,geojson为多条数据")
    @GetMapping(value = "/getLocationPacByLngLat/{x}/{y}")
    public ResponseMsg getLocationByLngLat(@PathVariable("x") float x,
                                           @PathVariable("y") float y) {
        try {
            List<Map<String, Object>> rsList = locationService.getLocationPacByLngLat(x,y);
            return ResponseMsg.success("查询成功，共获取" + rsList.size() + "条数据").addData(rsList);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return ResponseMsg.error("系统错误，e：" + e.getMessage());
        }
    }
}
